<?php
namespace appin\cms\controller\admin;

use appin\cms\Admin;
use appin\cms\model\Adv;
use appin\cms\model\Pos;
use appin\cms\validate\AdvertValidate;

class Advert extends Admin
{
    protected $model = Adv::class;
    protected $validate = AdvertValidate::class;

    /**
     * 广告位管理
     * @return \think\response\View
     */
    public function position()
    {
        return $this->render();
    }

    /**
     * 列出广告位
     * @return \think\response\Json
     */
    public function listpos()
    {
        $this->model = Pos::class;
        $data = $this->modelDatas();
        return json(['state' => 'success', 'total' => $data['total'], 'rows' => $data['rows']]);
    }

    /**
     * 新增或者删除广告位
     * @return \think\response\View
     */
    public function savepos()
    {
        $this->model = Pos::class;
        return parent::save();
    }

    /**
     * 广告位数据提交
     * @return \think\response\Json|\think\response\View
     */
    public function posdata()
    {
        $this->model = Pos::class;
        return parent::savedata();
    }

    /**
     * 删除广告位
     * @return \think\response\Json
     * @throws \app\common\exception\AdminException
     */
    public function delpos()
    {
        $this->model = Pos::class;
        return parent::delete();
    }

    /**
     * 广告管理
     * @return \think\response\View
     */
    public function index()
    {
        return $this->render('',[
            'pos_id' => $this->request->param('pos_id', 0, 'intval')
        ]);
    }

    /**
     * 列出广告数据
     * @return \think\response\Json
     */
    public function datas()
    {
        $pos_id = $this->request->param('pos_id', 0, 'intval');
        $data = $this->modelDatas($this->page, $this->limit,'sort desc,id desc', 'pos_id = '.$pos_id);
        return json(['state' => 'success', 'total' => $data['total'], 'rows' => $data['rows']]);
    }
}